package com.cgw.room;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.cgw.room.bean.StatusItemBean;
import com.cgw.room.util.FileOperator;
import com.cgw.room.widget.BaseFragment;
import com.cgw.room.widget.StatusItemAdapter;

import java.util.ArrayList;
import java.util.List;

public class StatusFragment extends BaseFragment {
    private String StatusNameLeft[] = new String[]{"基本状态 : ","步态信息 : ","最大前进速度 : ","最大后退速度 : ","位姿信息 : ","速度信息 : "};
    private String StatusNameRight[] = new String[]{"运行里程数 : ", "narrow_walk : ","pose_safe_flag  : ","joint_limit_flag : ","auto_charge_state : ","pos_ctrl_state : "};

    private String BaseStatus[] = new String[]{"趴下状态","正在起立状态","初始站立状态","力控站立状态","踏步状态","正在趴下状态","软急停/摔倒状态"};
    private String GaitStatus[] = new String[]{"行走步态","越障步态","斜坡步态","跑步步态","U1","U2","楼梯步态","楼梯步态（累积帧模式）","45°楼梯步态（累积帧模式）"};
    private Button mBtnBack = null;
    private Button mBtnOK = null;
    private Button mBtnCancel = null;
    private ListView mListViewLeft = null;
    private ListView mListViewRight = null;
    private TextView mTextStatistics = null;
    List<StatusItemBean> mItemsLeft = null;
    List<StatusItemBean> mItemsRight = null;
    StatusItemAdapter mAdapterLeft = null;
    StatusItemAdapter mAdapterRight = null;

    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_status, null);
        fragmentActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        mBtnBack = view.findViewById(R.id.btn_status_back);
        mBtnBack.setOnClickListener(v -> {
            ((DogControllerActivity)fragmentActivity).onStatusCancel();
        });
        mBtnCancel = view.findViewById(R.id.btn_status_cancel);
        mBtnCancel.setOnClickListener(v -> {
            ((DogControllerActivity)fragmentActivity).onStatusCancel();
        });
        mBtnOK = view.findViewById(R.id.btn_status_ok);
        mBtnOK.setOnClickListener(v -> {
            ((DogControllerActivity)fragmentActivity).onStatusOK();
        });
        mListViewLeft = view.findViewById(R.id.listview_status_left);
        mListViewRight = view.findViewById(R.id.listview_status_right);
        mTextStatistics = view.findViewById(R.id.textview_statistics);

        genDefaultItemsStatus();
        if (mItemsLeft != null) {
            mAdapterLeft = new StatusItemAdapter(fragmentActivity, R.layout.item_status, mItemsLeft);
            mListViewLeft.setAdapter(mAdapterLeft);
            Log.i("[List]", "Len = " + mItemsLeft.size());
            mAdapterLeft.setNotifyOnChange(true);
        }
        if (mItemsRight != null) {
            mAdapterRight = new StatusItemAdapter(fragmentActivity, R.layout.item_status, mItemsRight);
            Log.i("[List]", "Len = " + mItemsRight.size());
            mListViewRight.setAdapter(mAdapterRight);
            mAdapterRight.setNotifyOnChange(true);
        }

        {
            if (mListViewRight.getAdapter() == null) {
                Log.e("ListViewDebug", "适配器未设置");
            } else {
                Log.d("ListViewDebug", "适配器已设置");
            }
            Log.d("ListViewDebug", "数据量: " + mAdapterRight.getCount());
            Log.d("ListViewDebug", "ListView可见性: " + mListViewRight.getVisibility());
            Log.d("ListViewDebug", "ListView高度: " + mListViewRight.getHeight());
        }

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();


    }
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mAdapterLeft.getCount() == 0 || mAdapterRight.getCount() == 0)
                return;

//            public int BasicState;
//            public int GaitState;
//            public float MaxForwardVel;
//            public float MaxBackwardVel;
//            public float LegOdonPos[];
//            public float LegOdonVel[];
//            public float RobotDistance;
//            public int TouchState;
//            public boolean NarrowWalk;
//            public boolean PosSafeFlag;
//            public boolean JointLimitFlag;
//            public int AutoChargeState;
//            public int PosCtrlState;

//            mItemsLeft.clear();
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[0], String.valueOf(MainApp.CurrentMotionState.BasicState)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[1], String.valueOf(MainApp.CurrentMotionState.GaitState)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[2], String.valueOf(MainApp.CurrentMotionState.MaxForwardVel)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[3], String.valueOf(MainApp.CurrentMotionState.MaxBackwardVel)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[4], String.valueOf(MainApp.CurrentMotionState.LegOdonPos)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[5], String.valueOf(MainApp.CurrentMotionState.LegOdonVel)));
//            mItemsLeft.add(new StatusItemBean(StatusNameLeft[6], String.valueOf(MainApp.CurrentMotionState.RobotDistance)));
//            mAdapterLeft = new StatusItemAdapter(fragmentActivity, R.layout.item_status, mItemsLeft);
//            mListViewLeft.setAdapter(mAdapterLeft);
//            mAdapterLeft.notifyDataSetChanged();

            if (MainApp.CurrentMotionState.BasicState < BaseStatus.length)
                mAdapterLeft.getItem(0).setValue(BaseStatus[MainApp.CurrentMotionState.BasicState]);
            else
                mAdapterLeft.getItem(0).setValue(String.valueOf(MainApp.CurrentMotionState.BasicState));

            if (MainApp.CurrentMotionState.GaitState < GaitStatus.length)
                mAdapterLeft.getItem(1).setValue(GaitStatus[MainApp.CurrentMotionState.GaitState]);
            else
                mAdapterLeft.getItem(1).setValue(String.valueOf(MainApp.CurrentMotionState.GaitState));

            mAdapterLeft.getItem(2).setValue(String.valueOf(MainApp.CurrentMotionState.MaxForwardVel));
            mAdapterLeft.getItem(3).setValue(String.valueOf(MainApp.CurrentMotionState.MaxBackwardVel));
            mAdapterLeft.getItem(4).setValue("(" + MainApp.CurrentMotionState.LegOdonPos[0] + "," + MainApp.CurrentMotionState.LegOdonPos[1] + "," + MainApp.CurrentMotionState.LegOdonPos[2] + ")");
            mAdapterLeft.getItem(5).setValue("(" + MainApp.CurrentMotionState.LegOdonVel[0] + "," + MainApp.CurrentMotionState.LegOdonVel[1] + "," + MainApp.CurrentMotionState.LegOdonVel[2] + ")");
            mAdapterLeft.getItem(6).setValue(String.valueOf((int)(MainApp.CurrentRcsData.rcs_state[0])));
            mAdapterLeft.notifyDataSetChanged();

            mAdapterRight.getItem(0).setValue(String.valueOf(MainApp.CurrentMotionState.RobotDistance));
            mAdapterRight.getItem(1).setValue(String.valueOf(MainApp.CurrentMotionState.NarrowWalk));
            mAdapterRight.getItem(2).setValue(String.valueOf(MainApp.CurrentMotionState.PosSafeFlag));
            mAdapterRight.getItem(3).setValue(String.valueOf(MainApp.CurrentMotionState.JointLimitFlag));
            mAdapterRight.getItem(4).setValue(String.valueOf(MainApp.CurrentMotionState.AutoChargeState));
            mAdapterRight.getItem(5).setValue(String.valueOf(MainApp.CurrentMotionState.PosCtrlState));
            mAdapterRight.notifyDataSetChanged();
        }
    }
    private void genDefaultItemsStatus(){
        mItemsLeft = new ArrayList<>();
        for (String s : StatusNameLeft) {
            mItemsLeft.add(new StatusItemBean(s, ""));
        }
        mItemsLeft.add(new StatusItemBean("IsNavMode : ", ""));
        mItemsRight = new ArrayList<>();
        for (String s : StatusNameRight) {
            mItemsRight.add(new StatusItemBean(s, ""));
        }
    }
    public void setStatisticsString(String s){
        mTextStatistics.setText(s);
    }
}
